package com.jrtc.backend.controller;

import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.bo.Patientfile;
import com.jrtc.service.PatientfileService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("patientfile")
public class PatientfileController {
    /**
     * 服务对象
     */
    @Resource
    private PatientfileService patientfileService;

    /**
     * 患者文件上传
     * */
    @PostMapping("/patientFileUpload")
    public ResultDTO patientFileUpload(List<MultipartFile> file, Integer id){
        List<String> urlList;
        try {
            urlList = patientfileService.patientFileUpload(file, id);
        } catch (Exception e){
            return ResultDTOBuilder.failure(e.getMessage());
        }

        return ResultDTOBuilder.success(urlList);
    }

    /**
     * 查询患者文件列表
     * */
    @PostMapping("/selPatientFileUpload")
    public ResultDTO selPatientFileUpload(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());

        List<Patientfile> patientfileList = patientfileService.selPatientFileUpload(id);
        return ResultDTOBuilder.success(patientfileList);
    }

    /**
     * 删除文件
     * */
    @PostMapping("/isDeletePatientFile")
    public ResultDTO isDeletePatientFile(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        int i = patientfileService.isDeletePatientFile(id);
        if (i > 0){
            return ResultDTOBuilder.success("删除成功");
        }
        else {
            return ResultDTOBuilder.failure("删除失败");
        }
    }
}